import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.jcp.xml.dsig.internal.dom.DOMUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-09-02
 * Time: 11:47
 */

class  Student{
    private String name="zhangsan";
    public int age=78;

    public Student() {
        System.out.println("公开的不带参数的构造方法");
    }
    private Student(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("私有带两个参数的构造方法");
    }
    private void eat(){
        System.out.println("一个私有的方法");
    }
    private void sleep(String s){
        System.out.println("一个私有的方法"+s);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test1 {
    public static void main(String[] args) throws ClassNotFoundException {
        Student s1=new Student();
        Class c1=s1.getClass();
        System.out.println(c1);

        Class c2=Student.class;
        System.out.println(c2);

        Class c3=Class.forName("Student");
        System.out.println(c3);
        System.out.println("!@#$%^&*(");
        reflectNewInstance();
        System.out.println("!@#$%^&*(");
        reflectPrivateConstructor();
        System.out.println("!！！！！！！！！！！！！");
        reflectPrivateField();
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        reflectPrivateMethod();
    }
    public static void reflectNewInstance(){
        try{
            Class<?> classStudent=Class.forName("Student");
            Student student=(Student) classStudent.newInstance();
            System.out.println("利用反射创建对象"+student);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void reflectPrivateConstructor(){
        try{
            Class<?> classStudent=Class.forName("Student");
            Constructor<?> declaredConstructorStudent=classStudent.getDeclaredConstructor(String.class,int.class);//获取私有构造方法
            declaredConstructorStudent.setAccessible(true);//设置为true后可修改访问权限
            Student student=(Student) declaredConstructorStudent.newInstance("wanger",9);
            System.out.println("修改私有构造方法后"+student);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void reflectPrivateField(){
        try{
            Class<?> classStudent=Class.forName("Student");
            Field field=classStudent.getDeclaredField("name");
            field.setAccessible(true);
            Student student=(Student) classStudent.newInstance();
            field.set(student,"zgongguo");
            System.out.println("修改私有属性后"+field.get(student));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void reflectPrivateMethod(){
        try{
            Class<?> classStudent=Class.forName("Student");
            Method methodStudent=classStudent.getDeclaredMethod("sleep",String.class);
            methodStudent.setAccessible(true);
            Student student=(Student) classStudent.newInstance();
            methodStudent.invoke(student,"修改后的参数");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

enum TestEnum{
    RED("red",0),BLACK("black",1),WHITE,GREEN;

    private TestEnum(String red, int i) {
    }

    TestEnum() {

    }

    public static void main(String[] args) {
        TestEnum testEnum1=TestEnum.BLACK;
        switch (testEnum1){
            case RED:
                System.out.println("RED");
                break;
            case BLACK:
                System.out.println("BLACK");
                break;
            case WHITE:
                System.out.println("WHITE");
                break;
        }
        TestEnum[] testEnum2=TestEnum.values();
        for (int i = 0; i <testEnum2.length; i++) {
            System.out.println(testEnum2[i]+" "+testEnum2[i].ordinal());
        }
        System.out.println(TestEnum.valueOf("GREEN"));
        System.out.println(BLACK.compareTo(GREEN));
    }
}


